<?php

namespace App\Http\Controllers\Back;

use App\Http\Controllers\Controller;
use App\Http\Requests\PartnerRequest;
use App\Model\Partner;
use App\User;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

/**
 * 城市合伙人控制器
 * Class PartnerController
 * @package App\Http\Controllers\Back
 */
class PartnerController extends Controller
{

    /**
     * 获取当前用户下所有城市合伙人
     * @param Request $request
     * @param Partner $partner
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request, Partner $partner)
    {
        $partners = $partner->getPaginate($request);
        return $this->renderJson($partners);
    }

    /**
     * @param $id
     * @param Request $request
     * @param Partner $partner
     * @return \Illuminate\Http\Response
     * @throws AuthorizationException
     */
    public function show($id, Request $request, Partner $partner)
    {
//        if ($request->user()->type != 1) {
//            throw new AuthorizationException();
//        }
        return $this->renderJson($partner->findOrFail($id));
    }

    /**
     * 添加城市合伙人
     * @param PartnerRequest $request
     * @param User $user
     * @param Partner $partner
     * @return \Illuminate\Http\Response
     */
    public function store(PartnerRequest $request, Partner $partner, User $user)
    {
        $u = new User;
        if ($u->where('phone','=',$request->input('admin_mobile_phone'))->first()){
            return $this->fail('该手机号已经被注册过了');
        }
        //创建登录用户
        $user->account = request('admin_mobile_phone');
        $user->phone = request('admin_mobile_phone');
        $user->password = bcrypt('123456');
        $user->type = 1;//城市合伙人
        $user->is_enabled = 0;

        $partner->fill($request->all());
        $partner->pid = $request->user()->id;
        try {
            DB::beginTransaction();
            $user->save();
            $partner->user_id = $user->id;
            $partner->save();
            DB::commit();
        } catch (\Exception $e) {
            DB::rollBack();
            return $this->error('添加失败');
        }
        return $this->success();
    }

    public function update(PartnerRequest $request, $id, Partner $partner)
    {
        $partner = $partner->findOrFail($id);
        $partner->fill($request->all());
        if ($partner->save()) {
            return $this->renderJson();
        }
        return $this->error('修改失败');
    }

    public function destroy($id, Partner $partner)
    {
        $partner = $partner->findOrFail($id);
        try {
            $partner->delete();
        } catch (\Exception $e) {
            return $this->error('删除失败');
        }
        return $this->success();
    }

    public function stop(Request $request, $id, Partner $partner)
    {
        $this->validate($request, [
            'status' => 'boolean'
        ]);
        $status = $request->input('status');
        $p = $partner->findOrFail($id);
        $p->status = $status;
        $p->user->is_enabled = $status;
        if ($p->save()) {
            return $this->success();
        }
        return $this->error('修改失败');
    }
}
